精準延時呼叫
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int offset = 10 * 1000;//間隔時間10s
long triggerAtTime = SystemClock.elapsedRealtime() + offset;
Intent i = new Intent(this, ForegroundService.class);
PendingIntent mPendingIntent = PendingIntent.getService(this, 0, i, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, mPendingIntent);
Log.i(TAG, "API 23+ ");
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, mPendingIntent);
Log.i(TAG, "API 19+ ");
} else {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, offset, mPendingIntent);
Log.i(TAG, "API 19- ");
}
開機啟動服務
AndroidManifest加入
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
}else {
context.startService(i);
}
連結http網頁需在AndroidManifest加入
android:usesCleartextTraffic="true"
android 6 以前的webView無法吃ES6的JS